package com.kit.data.kitopeso.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;


import androidx.core.content.ContextCompat;

import com.google.gson.JsonObject;
import com.kit.data.kitopeso.KitoApp;
import com.kit.data.kitopeso.PathMaping;
import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.base.BaseActivity;
import com.kit.data.kitopeso.base.BaseDialog;
import com.kit.data.kitopeso.base.BaseParams;
import com.kit.data.kitopeso.databinding.KitoRepayStateActivityBinding;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.net.RxHttp;
import com.kit.data.kitopeso.net.U;
import com.kit.data.kitopeso.obj.KitoOrderRepayState;
import com.kit.data.kitopeso.tools.AESCryptUtilJar;
import com.kit.data.kitopeso.tools.DateUtils;
import com.kit.data.kitopeso.tools.JsonParse;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.SpUtils;
import com.kit.data.kitopeso.tools.ViewUtils;

import java.util.HashMap;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class KitoOrderRepayStateActivity1 extends BaseActivity<KitoRepayStateActivityBinding> {


    KitoOrderRepayState repayState;

    String thirdOrderId;

    String repayDate;

    @Override
    protected KitoRepayStateActivityBinding initViewBinding(LayoutInflater inflater) {
        return KitoRepayStateActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {

        thirdOrderId = getIntent().getStringExtra("thirdOrderId");

        repayDate=getIntent().getStringExtra("repayDate");

        if(TextUtils.isEmpty(repayDate)){
            repayDate= DateUtils.getDate2();
        }

        binding.tvBk.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            finish();
        });
        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P131, "");
            startActivity(new Intent(getActivity(), KitoCustomerServiceActivity.class));
        });
        binding.tvBtn.setOnClickListener(v -> {
            if (binding.tvBtn.isSelected()) {
                ViewUtils.viewScale(v);
                PU.userBuriedPoint(PU.P133, "");
                toNext();
            }
        });
        binding.tvRefresh1.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P132, "");
            queryPaymentStatus();
        });

        binding.tvBtn.setSelected(false);

    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P129;
        UP_TYPE2 = PU.P130;
        queryPaymentStatus();
    }

    @SuppressLint("SetTextI18n")
    void loadData() {
        if (getActivity() != null && !isFinishing()) {
            if (repayState != null) {

                String currency = SpUtils.get_string(this, SpUtils.KEY_CURRENCY, "");
                if (TextUtils.isEmpty(currency)) {
                    currency = getResources().getString(R.string.ghs);
                }

                if (repayState.getAmount().contains(currency)) {
                    binding.tvAmount.setText(repayState.getAmount());
                } else {
                    binding.tvAmount.setText(repayState.getAmount() + currency.toUpperCase());
                }

                binding.tvTerm.setText(repayDate);

                binding.tvBtn.setSelected(true);

                binding.tvIcon.setImageResource(R.mipmap.repay_state_icon3);
                binding.tvTitle.setText(getResources().getString(R.string.order_repay_state_title3));
                binding.tvTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.repay_state_line3);
                binding.tvTitle.setTextColor(Color.parseColor("#FF5C3B"));

                binding.tvRefresh1.setVisibility(View.GONE);

                binding.tvHit3.setVisibility(View.VISIBLE);

                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                layoutParams2.topMargin=ResUtils.d(R.dimen.dp_10);
                layoutParams2.addRule(RelativeLayout.BELOW,R.id.tvHit3);
                binding.tvDate.setLayoutParams(layoutParams2);
                binding.tvDate.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(repayState.getFailedTime())) {
                    binding.tvDate.setText(repayState.getFailedTime());
                } else {
                    binding.tvDate.setText(repayState.getCreateTime());
                }

                binding.tvHit1.setVisibility(View.VISIBLE);
                binding.tvHit1.setText(ResUtils.s(R.string.order_repay_state_hit2));

                binding.tvIddLayout.setVisibility(View.VISIBLE);
                binding.tvId.setText(repayState.getTransactionId());
                binding.tvIdTitle.setText(ResUtils.s(R.string.order_repay_state_title9));

                binding.tvHit4.setVisibility(View.VISIBLE);

                binding.tvHit2.setVisibility(View.GONE);

                binding.tvBtnTxt.setText(ResUtils.s(R.string.order_repay_state_btn));


            }
        }
    }

    void toNext() {
        if (repayState != null) {
            switch (repayState.getStatus()) {
                case "PROCESSING":
                case "FAILED":
                    finish();
                    break;
                case "SUCCEEDED":
                    boolean isAllPermissionsGranted = true;
                    if (KitoApp.getInstance().getAppInfo().getPermission() != null) {
                        for (String permission : KitoApp.getInstance().getAppInfo().getPermission()) {
                            if (permission.equals("android.permission.READ_SMS") && ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
                                isAllPermissionsGranted = false;
                                break;
                            }
                        }
                    }

                    if (isAllPermissionsGranted) {
                        userProcess(() -> {
                            boolean isCheckSteps = checkSteps();
                            if (!isCheckSteps) {
                                startActivity(new Intent(getActivity(), KitoApplyActivity.class));
                            }
                            finish();
                        }, true);
                    } else {
                        startActivity(new Intent(getActivity(), KitoPermissionDesActivity.class));
                    }
                    break;
            }
        }
    }

    void queryPaymentStatus() {
        if (binding != null) {
            showLoading("");
            JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[18]);
            json.addProperty("thirdOrderId", thirdOrderId);

            HashMap<String, String> heads = new HashMap<>();
            heads.put("product", U.URL_PRODUCT);
            if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
                heads.put("country", U.URL_COUNTRY);
            }
            RxHttp.post(U.commonUrl, AESCryptUtilJar.encryptDefault(json.toString()), heads)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<>() {
                        @Override
                        public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                            if (binding != null) {
                                hideLoading();
                                if (!TextUtils.isEmpty(s)) {
                                    String json1 = AESCryptUtilJar.decryptDefault(s);
                                    LogUtil.e("me", "queryPaymentStatus json->" + json1);
                                    if (JsonParse.getCode(json1)) {
                                        repayState = JsonParse.queryPaymentStatusJson2Obj(json1);
                                        loadData();
                                    } else {
                                        showToastDialog(R.mipmap.hit_icon, JsonParse.getMsg(getActivity(), json1));
                                        finish();
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            if (binding != null) {
                                hideLoading();
                                showFailedDialog(new BaseDialog.BtnClickListener() {
                                    @Override
                                    public void btnOnClickOk() {
                                        queryPaymentStatus();
                                    }

                                    @Override
                                    public void btnOnClickCancel() {

                                    }

                                    @Override
                                    public void btnOnClickColse() {

                                    }
                                });
                            }
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }


}
